/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.CooperBillItemDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class CooperBillItemDaoImpl extends BaseDaoImpl
		implements
			CooperBillItemDao {
	public List getList(long billId) {
		String sql = "from com.minierp.model.CooperBillItem s where s.billId="
				+ billId + " order by s.id";
		return this.getHibernateTemplate().find(sql);
	}

	public int delete(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.CooperBillItem where id=:id";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public BigDecimal getCooperedAmount(long bbItemId) {
		String sql = "select sum(i.amount) from com.minierp.model.CooperBill b,com.minierp.model.CooperBillExt be,com.minierp.model.CooperBillItem i where b.id=i.billId and b.id=be.id and i.bbItemId="
				+ bbItemId + " and be.enabled=\'Y\'";
		List list = this.getHibernateTemplate().find(sql);
		return !list.isEmpty() && list.get(0) != null ? new BigDecimal(list
				.get(0).toString()) : new BigDecimal(0);
	}

	public BigDecimal getCost(long businessBillId) {
		String sql = "select sum(i.cost) from com.minierp.model.CooperBill b,com.minierp.model.CooperBillExt be,com.minierp.model.CooperBillItem i,com.minierp.model.BusinessBill bb where b.id=i.billId and b.id=be.id and i.bbCode=bb.billCode and bb.id="
				+ businessBillId
				+ " and be.examined=\'Y\'"
				+ " and be.enabled=\'Y\'";
		Session sess = null;

		BigDecimal arg7;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			List list = q.list();
			if (list.isEmpty() || list.get(0) == null) {
				arg7 = new BigDecimal(0);
				return arg7;
			}

			arg7 = new BigDecimal(list.get(0).toString());
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg7;
	}

	public List getListForBusinessBill(long businessBillId, boolean justExam) {
		String sql = "select b,i,e.estName,be.examined from com.minierp.model.CooperBill b,com.minierp.model.CooperBillExt be,com.minierp.model.CooperBillItem i,com.minierp.model.BusinessBill bb,com.minierp.model.Establishment e where b.id=i.billId and b.id=be.id and b.cooperatorId=e.id and i.bbCode=bb.billCode and bb.id="
				+ businessBillId;
		if (justExam) {
			sql = sql + " and be.examined=\'Y\'";
		}

		sql = sql + " and be.enabled=\'Y\'" + " order by b.cooperDate,i.id";
		return this.getHibernateTemplate().find(sql);
	}
}